package com.shd.edanpei.presenter;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.shd.edanpei.R;
import com.shd.edanpei.ui.activity.OnlinePayActivity;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.LendingBean;
import com.shd.edanpei.presenter.contract.SureLoanInfocontract;
import com.shd.edanpei.customer.activity.PayServiceActivity;
import com.shd.edanpei.util.GsonUtil;
import com.shd.edanpei.util.ToastUtil;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/7/27.
 * yang-wen-wen
 */

public class SureLoanInfoPresenter extends BasePresenterImpl<SureLoanInfocontract.SureLoanInfoIView>
        implements SureLoanInfocontract.SureLoanInfoIPresenter {

    private LendingBean mData = new LendingBean();
    private Dialog dialog;

    public SureLoanInfoPresenter(SureLoanInfocontract.SureLoanInfoIView mView) {
        super(mView);
    }

    @Override
    public void setName(final String name) {
        mData.setName(name);
    }

    @Override
    public void setBankName(final String bankName) {
        mData.setBankName(bankName);
    }

    @Override
    public void setBankCard(final String card) {
        mData.setBankCard(card);
    }

    @Override
    public void setBankBranch(final String branch) {
        mData.setOpenAccountBank(branch);
    }

    @Override
    public void setPhone(final String phone) {
        mData.setPhone(phone);
    }

    @Override
    public void getLendingInfo(final int orderId) {
        ApiCreator.getApi().getLendingInfo(orderId)
                .compose(RxTransformer.<LendingBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<LendingBean>() {
                    @Override
                    protected void onSuccess(LendingBean data) {
                        mData = data;
                        mBaseIView.setLendingInfo(mData);
                    }
                });
    }

    @Override
    public void submitLendingInfo(final int orderId, final Bundle bundle) {
        mData.setOrderId(String.valueOf(orderId));
        final Map<String, Object> map = checkParameter();
        if (map != null && !map.isEmpty()) {
//            if (dialog == null) {
//                View view = View.inflate(mBaseIView.getContext(), R.layout.choose_pay_type, null);
//                view.findViewById(R.id.tv_online_pay).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dialog.dismiss();
//                        mBaseIView.showLoading("");
//                        //线上支付
//                        ApiCreator.getApi().submitLendingInfo(map)
//                                .compose(RxTransformer.<NoDataResponse>errorHandle())
//                                .observeOn(AndroidSchedulers.mainThread())
//                                .subscribe(new BaseObserver<NoDataResponse>() {
//                                    @Override
//                                    protected void onSuccess(NoDataResponse data) {
//                                        //线上支付服务费
//                                        mBaseIView.startActivity(OnlinePayActivity.class, bundle);
//                                        mBaseIView.getContext().finish();
//
//                                    }
//
//                                    @Override
//                                    public void onFinish() {
//                                        mBaseIView.stopLoading();
//                                    }
//                                });
//                    }
//                });
//                view.findViewById(R.id.tv_offline_pay).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dialog.dismiss();
//                        mBaseIView.showLoading("");
//                        //线下支付
//                        ApiCreator.getApi().submitLendingInfo(map)
//                                .compose(RxTransformer.<NoDataResponse>errorHandle())
//                                .observeOn(AndroidSchedulers.mainThread())
//                                .subscribe(new BaseObserver<NoDataResponse>() {
//                                    @Override
//                                    protected void onSuccess(NoDataResponse data) {
//                                        //线下支付服务费
//                                        mBaseIView.startActivity(PayServiceActivity.class, bundle);
//                                        mBaseIView.getContext().finish();
//                                    }
//
//                                    @Override
//                                    public void onFinish() {
//                                        mBaseIView.stopLoading();
//                                    }
//                                });
//
//                    }
//                });
//                view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dialog.dismiss();
//                    }
//                });
//                dialog = new Dialog(mBaseIView.getContext(), R.style.dialog);
//                dialog.setContentView(view);
//                Window window = dialog.getWindow();
//                window.setGravity(Gravity.BOTTOM);
//                WindowManager.LayoutParams params = window.getAttributes();
//                params.width = WindowManager.LayoutParams.MATCH_PARENT;
//                params.height = WindowManager.LayoutParams.WRAP_CONTENT;
//                window.setAttributes(params);
//            }
//            dialog.show();

            ApiCreator.getApi().submitLendingInfo(map)
                                .compose(RxTransformer.<NoDataResponse>errorHandle())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new BaseObserver<NoDataResponse>() {
                                    @Override
                                    protected void onSuccess(NoDataResponse data) {
                                        //线下支付服务费
                                        mBaseIView.startActivity(PayServiceActivity.class, bundle);
                                        mBaseIView.getContext().finish();
                                    }

                                    @Override
                                    public void onFinish() {
                                        //mBaseIView.stopLoading();
                                    }
                                });

        } else {
            ToastUtil.showToast("请完善审核信息");
        }


    }

    private Map checkParameter() {
        if (isNull(mData.getName(), "请输入姓名")) return null;
        if (isNull(mData.getBankName(), "请输入银行名称")) return null;
        if (isNull(mData.getBankCard(), "请输入银行卡号")) return null;
        if (isNull(mData.getOpenAccountBank(), "请输入开户行")) return null;
        if (isNull(mData.getPhone(), "请输入手机号码")) return null;
        return GsonUtil.toMap(mData);

    }

    private boolean isNull(String str, String toast) {
        if (TextUtils.isEmpty(str)) {
            ToastUtil.showToast(toast);
            return true;
        }
        return false;
    }

}
